//
//  TravelViewController.m
//  庐山通
//
//  Created by zcj on 15-10-29.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "TravelViewController.h"
#import "RPSlidingMenuLayout.h"
#import "RPSlidingMenuCell.h"
#import "Grouble.h"
#import "SelectFindHttp.h"
#import "Travel.h"
#import "DecatilViewController.h"

@interface TravelViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,weak)UICollectionView * collection;
@property (nonatomic,strong)NSMutableArray * myTravewData;
@end

@implementation TravelViewController

- (NSMutableArray *)myTravewData
{
    if (_myTravewData == nil) {
        _myTravewData = [[NSMutableArray alloc]init];
    }
    return _myTravewData;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNavBar];
    [self loadData];
    [self createCollectionView];
    
    // Do any additional setup after loading the view.
}
- (void)loadData
{
    [MBProgressHUD showMessage:@"正在加载"];
    
    NSString * getUrl = [NSString stringWithFormat:@"https://api.deyi.com/frontpage/hotspot/?page=1&ver=5&av=4.3.1&hash=dd595273078a4b6f1aa689cb4aaaebe748e32320"];
    
    [SelectFindHttp postWirhUrl:getUrl parms:nil success:^(id json) {
        // 把数据转换成array
        //ZCJLog(@"%@",json);
        NSArray * myTemp = json[@"pic"];
        NSArray * myDataArray =[Travel objectArrayWithKeyValuesArray:myTemp];
        
        for (Travel * travel in myDataArray) {
            //ZCJLog(@"%@",travel);
            [self.myTravewData addObject:travel];
            
        }
       // ZCJLog(@"-----%@",myDataArray);
        
//        
        [self.collection reloadData];
        [MBProgressHUD hideHUD];
        //self.myTravewData.count;
        [MBProgressHUD showSuccess:@"加载成功"];
    } failture:^(id error) {
        [MBProgressHUD hideHUD];
        ZCJLog(@"%@",error);
    }];

//    for (int i = 0; i < 19; i ++) {
//            }
    
}
- (void)setNavBar
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancle)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleBordered target:self action:@selector(enen)];
    self.title = @"旅游景点";
    

}
- (void)enen
{
    ZCJLog(@"jdlfkj");
}
- (void)cancle
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
// 创建一个collection
- (void)createCollectionView
{
    // 实现slided一个布局
    RPSlidingMenuLayout * rpSlidingLayout = [[RPSlidingMenuLayout alloc]init];
    UICollectionView * collection = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:rpSlidingLayout];
    collection.backgroundColor = [UIColor whiteColor];
    collection.dataSource = self;
    collection.delegate = self;
    //注册cell
    [collection registerClass:[RPSlidingMenuCell class] forCellWithReuseIdentifier:@"RPSlidingCellIdentifier"];
    [self.view addSubview:collection];
    self.collection = collection;
    

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  self.myTravewData.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
     RPSlidingMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RPSlidingCellIdentifier" forIndexPath:indexPath];
    Travel * travel = self.myTravewData[indexPath.row];
    
    cell.textLabel.text = travel.username;
    cell.detailTextLabel.text = travel.title;
    
    [cell.backgroundImageView setImageWithURL:[NSURL URLWithString:travel.avatar] placeholderImage:[UIImage imageNamed:@"lushan"]];
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DecatilViewController * decatilVC = [[DecatilViewController alloc]init];
    MyNavigation * nav = [[MyNavigation alloc]initWithRootViewController:decatilVC];
    [self presentViewController:nav animated:YES completion:nil];
    ZCJLog(@"didSelectItemAtIndexPath");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
